<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class Application_Models_UserModel {

    private $_pkUser;
    private $_user;
    private $_password;
    private $_fkWorkPeople;
    private $_fkTypeUser;

    function __construct() {
        
    }

    public function get_user() {
        return $this->_user;
    }

    public function set_user($_user) {
        $this->_user = $_user;
    }

    public function get_password() {
        return $this->_password;
    }

    public function set_password($_password) {
        $this->_password = $_password;
    }

    public function get_fkWorkPeople() {
        return $this->_fkWorkPeople;
    }

    public function get_fkTypeUser() {
        return $this->_fkTypeUser;
    }

    public function set_fkWorkPeople($_fkWorkPeople) {
        $this->_fkWorkPeople = $_fkWorkPeople;
    }

    public function set_fkTypeUser($_fkTypeUser) {
        $this->_fkTypeUser = $_fkTypeUser;
    }

    public function get_pkUser() {
        return $this->_pkUser;
    }

    public function set_pkUser($_pkUser) {
        $this->_pkUser = $_pkUser;
    }

    //funcionalidad

    public function _validateUser() {
        $db = new SuperDataBase();
        $query = "CALL sp_user_validate('$this->_user',@sa)";
        $result = $db->executeQuery($query);
        $query2 = "select @sa";
        $result2 = $db->executeQuery($query2);
        while ($row = $db->fecth_array($result2)) {
//            $this->array['Sale'][] = array("Resultado" =>$row['@a']);
            $value = $row['@sa'];
        }

        return $value;
    }

    public function _validateUserPassword() {
        $db = new SuperDataBase();
        $query = "CALL sp_user_validate_password('$this->_user','$this->_password',@sa)";
        $result = $db->executeQuery($query);
        $query2 = "select @sa";
        $result2 = $db->executeQuery($query2);
        while ($row = $db->fecth_array($result2)) {

            $value = $row['@sa'];
        }


        return $value;
    }

    /**
     * @author Jeison Cruz Yesan
     * @access public
     * @copyright Ghosts Solutions 21014
     * @return bool true exists false no exits
     */
    public function _searchUserValidate() {
        $db = new SuperDataBase();
        $query = "CALL sp_searchUserValidate('$this->_user',@s);";
        $db->executeQuery($query);
        $query1 = "select @s";
        $result = $db->executeQuery($query1);
        while ($row = $db->fecth_array($result)) {

            $value = $row['@s'];
        }
        return $value;
    }

    /**
     * @author Jeison Cruz Yesan
     * @access public
     * @copyright Ghosts Solutions 21014
     * Save User
     */
    public function _saveUser() {


        $db = new SuperDataBase();
        $query = "CALL sp_addUser($this->_fkWorkPeople,'$this->_user','$this->_password',$this->_fkTypeUser);";
        $result = $db->executeQuery($query);
////         $afectedRow= $db->affected_rows();
//         echo $db->affected_rows($query);
    }

    /**
     * @author Jeison Cruz Yesan
     * @access public
     * @copyright Ghosts Solutions 21014
     * Update User
     */
    public function _updateUser() {
        $db = new SuperDataBase();
        $query = "CALL sp_modify_user($this->_pkUser,$this->_fkTypeUser);";
        $result = $db->executeQuery($query);
    }

    /**
     * @author Jeison Cruz Yesan
     * @access public
     * @copyright Ghosts Solutions 21014
     * Delete User
     */
    public function _deleteUser() {
        $db = new SuperDataBase();
        $query = "CALL sp_delete_user($this->_pkUser);";
        $result = $db->executeQuery($query);
    }

    /**
     * @author Jeison Cruz Yesan
     * @access public
     * @copyright Ghosts Solutions 21014
     * List User
     */
    public function _listUser() {
        $objModelUser= new UserLogin();
        $db = new SuperDataBase();
        $query = "CALL sp_get_user(".$objModelUser->get_id()." )";
        $resul = $db->executeQuery($query);

//        $array = array();
        while ($row = $db->fecth_array($resul)) {
            $array[] = array("lastName" => $row['lastNames'],
                "names" => $row['names'],
                "user" => $row['user'], "description" => $row['description'],
                "id" => $row['idusuarios'], "pkTypeUser" => $row['pkTypeUser'],);
        }
        echo json_encode($array);
    }

    public function _login() {
        $db = new SuperDataBase();
        $query = "CALL sp_login('$this->_user','$this->_password')";
        $resul = $db->executeQuery($query);

        $array = array();
        while ($row = $db->fecth_array($resul)) {
            $array[] = array(
               
                "user" => $row['user'],
                "id" => $row['idusuarios'],
                "pkTypeUser" => $row['pkTypeUser'],
                "descriptionTypeUser" => $row['descriptionTypeUser'],
                "pkWorkPeople" => $row['pkWorkPeople'],
                 "names" => $row['names'],
                "lastName" => $row['lastNames'],
                "email"=>$row['email'],
                "document"=>$row['document']
            );
        }
        return $array;
    }
     

}
